from urllib import request
from xml.dom import minidom
from procutil import Task
from procdb import ContactObject
from procdb import ProcessObject
import uuid, re
import utils.googleSearch, utils.websiteScraper, utils.minisax

class DownloadCategory0:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("Url")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/category1.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            if url.hasChildNodes() and url.firstChild.nodeValue!='':
                task = Task();
                task.guid = uuid.uuid4()
                task.task_type = 1
                task.name = "Directory.DownloadCategory1"
                task.method = self.fn
                task.arg = loc + url.firstChild.nodeValue;
                task.Create()


class DownloadCategory1:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("Url")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/category2.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            if url.hasChildNodes() and url.firstChild.nodeValue!='':
                task = Task();
                task.guid = uuid.uuid4()
                task.task_type = 1
                task.name = "Directory.DownloadCategory2"
                task.method = self.fn
                task.arg = loc + url.firstChild.nodeValue
                task.Create()


class DownloadCategory2:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("Url")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/directory_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            if url.hasChildNodes() and url.firstChild.nodeValue!='':
                task = Task();
                task.guid = uuid.uuid4()
                task.task_type = 1
                task.name = "Directory.DownloadDirectoryLinks"
                task.method = self.fn
                task.arg = loc + url.firstChild.nodeValue
                task.Create()


class DownloadDirectoryLinks:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        comLists = doc.getElementsByTagName("CompanyList")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/directory.wmm?OrangeUrl=http://www.made-in-china.com"
        for com in comLists:
            if com.hasChildNodes() and com.firstChild.nodeValue!='':
                url = com.firstChild.nodeValue.strip(' \t\n')
                if url!='':
                    for i in range(1,4):
                        task = Task();
                        task.guid = uuid.uuid4()
                        task.task_type = 1
                        task.name = "Directory.DownloadDirectories"
                        task.method = self.fn
                        task.arg = loc + url.replace("1.html", "%s.html"%i)
                        task.Create()               
        

class DownloadDirectories:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("Url")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            if url.hasChildNodes() and url.firstChild.nodeValue!='':
                path = url.firstChild.nodeValue.strip(' \t\n')
                if path !='':
                    task = Task();
                    task.guid = uuid.uuid4()
                    task.task_type = 1
                    task.name = "Directory.DownloadCompanyLinks"
                    task.method = self.fn
                    task.arg = loc + path
                    task.Create()               
        

class DownloadCompanyLinks:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        '''premSite = doc.getElementsByTagName("PremiumSite")
        preSite_loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        urls = doc.getElementsByTagName("CompanyInfoUrl")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/profile.wmm?OrangeUrl=http://www.made-in-china.com"
        if len(urls)>0 and urls[0].hasChildNodes() and urls[0].firstChild.nodeValue!='':
            task = Task();
            task.guid = uuid.uuid4()
            task.task_type = 1
            task.name = "Directory.DownloadProfile"
            task.method = self.fn
            task.arg = loc + urls[0].firstChild.nodeValue
            task.Create()

        urls = doc.getElementsByTagName("ProductListUrl")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/products.wmm?OrangeUrl=http://www.made-in-china.com"
        if len(urls)>0 and urls[0].hasChildNodes() and urls[0].firstChild.nodeValue!='':
            task = Task();
            task.guid = uuid.uuid4()
            task.task_type = 1
            task.name = "Directory.DownloadProducts"
            task.method = self.fn
            task.arg = loc + urls[0].firstChild.nodeValue
            task.Create()
        '''
        urls = doc.getElementsByTagName("ContactDetailsUrl")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/contact.wmm?OrangeUrl=http://www.made-in-china.com"
        if len(urls)>0 and urls[0].hasChildNodes() and urls[0].firstChild.nodeValue!='':
            task = Task();
            task.guid = uuid.uuid4()
            task.task_type = 1
            task.name = "Directory.DownloadContactDetails"
            task.method = self.fn
            task.arg = loc + urls[0].firstChild.nodeValue
            task.Create()              

class DownloadProfiles:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        names = doc.getElementsByTagName("name")
        

class DownloadProducts:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            task = Task();
            task.guid = uuid.uuid4()
            task.task_type = 1
            task.name = "Directory.DownloadCompanies"
            task.method = self.fn
            if url.hasChildNodes():
                task.arg = loc + url.firstChild.nodeValue
                task.Create()

'''based on original made-in-china contacts layout'''
class DownloadContacts:
    def __init__(self,Fn):
        self.fn = Fn
        self.name =''
        self.email=''
        self.company=''
        self.phone=''
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        names = doc.getElementsByTagName("Name")
        if len(names)>0 and names[0].hasChildNodes() and names[0].firstChild.nodeValue!='':
            self.company = names[0].firstChild.nodeValue

        persons = doc.getElementsByTagName("Person")
        if len(persons)>0 :
            p_names = persons[0].getElementsByTagName("Name")
            if len(p_names)>0 and p_names[0].hasChildNodes() and p_names[0].firstChild.nodeValue!='':
                self.name = p_names[0].firstChild.nodeValue
        
        phones = doc.getElementsByTagName("Phone")
        if len(phones)>0 and phones[0].hasChildNodes() and phones[0].firstChild.nodeValue!='':
            self.phone = phones[0].firstChild.nodeValue

        emails = doc.getElementsByTagName("Email")
        if len(emails)>0 and emails[0].hasChildNodes() and emails[0].firstChild.nodeValue!='':
            self.email = emails[0].firstChild.nodeValue

        websites = doc.getElementsByTagName("Website")
        if len(websites)>0 and websites[0].hasChildNodes() and websites[0].firstChild.nodeValue!='':
            self.website = websites[0].firstChild.nodeValue

        values = {
            'name':self.name,
            'company':self.company,
            'email':self.email,
            'phone':self.phone,
            'website':self.website
            }

        obj = ContactObject("contact");
        obj.Add(values)


class DownloadContacts2:
    def __init__(self,Fn):
        self.fn = Fn
		
    def __call__(self,args):
        f = request.urlopen(args)
        doc = minidom.parse(f)
        urls = doc.getElementsByTagName("")
        loc = "http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com"
        for url in urls:
            task = Task();
            task.guid = uuid.uuid4()
            task.task_type = 1
            task.name = "Directory.DownloadCompanies"
            task.method = self.fn
            if url.hasChildNodes():
                task.arg = loc + url.firstChild.nodeValue
                task.Create()


if __name__ == "__main__":
    '''cat0 = DownloadCategory0("DownloadCategory1")
    cat0("http://test.myolive.net/samples/wmm/made-in-china.com/category0.wmm");
    cat2 = DownloadCategory2("DownloadCategory2")
    obj = ProcessObject("task");
    criteria = "arg like '%category1.wmm%'";
    ids = obj.GetObjectList(criteria);
    for id in ids:
        arg = obj.Get( id, "arg" );
        try:
            cat2( arg );
        except:
            pass;
            
    dlink = DownloadDirectoryLinks("DownloadDirectories")
    obj = ProcessObject("task");
    criteria = "arg like '%directory_links.wmm%'";
    ids = obj.GetObjectList(criteria);
    for id in ids:
        arg = obj.Get( id, "arg" );
        try:
            dlink( arg );
        except:
            pass;
    
    dlink("http://test.myolive.net/samples/wmm/made-in-china.com/directory_links.wmm?OrangeUrl=http://www.made-in-china.com/Electrical-Electronics-Catalog/Transducer.html")
    
    di = DownloadDirectories("DownloadCompanyLinks")
    obj = ProcessObject("task");
    criteria = "arg like '%directory.wmm%'";
    ids = obj.GetObjectList(criteria);
    for id in ids:
        arg = obj.Get( id, "arg" );
        try:
            di( arg );
        except:
            pass;

    di("http://test.myolive.net/samples/wmm/made-in-china.com/directory.wmm?OrangeUrl=http://www.made-in-china.com/manufacturers-directory/item3/Transducer-1.html")
    
    cl = DownloadCompanyLinks("DownloadContacts")
    obj = ProcessObject("task");
    criteria = "id>7321 and id<=8321 and arg like '%company_links.wmm%'";
    ids = obj.GetObjectList(criteria);
    for id in ids:
        arg = obj.Get( id, "arg" );
        try:
            cl( arg );
        except:
            pass;
    
    cl("http://test.myolive.net/samples/wmm/made-in-china.com/company_links.wmm?OrangeUrl=http://www.made-in-china.com/showroom/chinahapn")
    '''
       
    dc = DownloadContacts("DownloadContacts")
    obj = ProcessObject("task");
    criteria = "id>148394 and arg like '%contact.wmm%'";
    ids = obj.GetObjectList(criteria);
    for id in ids:
        arg = obj.Get( id, "arg" );
        try:
            dc( arg );
        except:
            pass;

    '''dc("http://test.myolive.net/samples/wmm/made-in-china.com/contact.wmm");'''

